<!-- 个人中心 -->
<template>
	<view class="dersonalCenter">
		<home :clickColorIndex="-1"></home>
		<view class="menu-container">
			<!-- 左侧菜单 -->
			<!-- <view class="menu-sidebar"> -->
			<view class="menu-left">
				<view class="menu-title">
					个人中心
				</view>
				<view class="left-menu">
					<view v-for="(item, index) in menuItems" :key="index" :class="{ active: index == activeMenu }"
						@click="activeMenu = index">
						{{ item }}
					</view>
				</view>
			</view>
			<view>
				<view class="right-content">
					<view v-if="activeMenu == 0">
						<view class="account-section">
							<view class="right-title">
								我的账户
							</view>
							<view class="user-info">
								<image class="user-avatar" :src="userinfo.avatar" mode="aspectFill"></image>
								<view class="user-text">
									<text>{{userinfo.name||userinfo.mobile}}</text>
									<text><uni-dateformat :date="userinfo.last_login_date" format="yyyy-MM-dd hh:mm"></uni-dateformat></text>
								</view>
							</view>
						</view>
						<!-- 我的订单部分 -->
						<view class="order-section">
							<view class="section-title">我的订单</view>
							<view class="order-icons">
								<view class="order-icon" @click="goToAllOrders">
									<uni-badge class="uni-badge-left-margin" :offset="offsetArr" :text="value"
										absolute="rightTop" size="small">
										<!-- <image class="icon" src="/static/carousel04.png" mode="aspectFit"></image> -->
										<view class="iconfont icon-quanbudingdan all-order"></view>
										<text class="icon-text">全部订单</text>
										<!-- <view class="badge" v-if="allOrdersCount > 0">{{ allOrdersCount }}</view> -->
									</uni-badge>

								</view>
								<view class="order-icon" @click="goToPendingPayment">
									<uni-badge class="uni-badge-left-margin" :offset="offsetArr" :text="value"
										absolute="rightTop" size="small">
										<!-- <image class="icon" src="/static/carousel04.png" mode="aspectFit"></image> -->
										<view class="iconfont icon-daifukuan01 all-order"></view>
										<text class="icon-text">待付款</text>
										<!-- <view class="badge" v-if="pendingPaymentCount > 0">{{ pendingPaymentCount }}</view> -->
									</uni-badge>

								</view>
								<view class="order-icon" @click="goToPendingDelivery">
									<uni-badge class="uni-badge-left-margin" :offset="offsetArr" :text="value"
										absolute="rightTop" size="small">
										<!-- <image class="icon" src="/static/carousel04.png" mode="aspectFit"></image> -->
										<view class="iconfont icon-shinshopdaifahuo all-order"></view>
										<text class="icon-text">待发货</text>
										<!-- <view class="badge" v-if="pendingDeliveryCount > 0">{{ pendingDeliveryCount }}</view> -->
									</uni-badge>
								</view>
								<view class="order-icon" @click="goToPendingReceipt">
									<uni-badge class="uni-badge-left-margin" :offset="offsetArr" :text="value"
										absolute="rightTop" size="small">
										<!-- <image class="icon" src="/static/carousel04.png" mode="aspectFit"></image> -->
										<view class="iconfont icon-daishouhuo all-order"></view>
										<text class="icon-text">待收货</text>
										<!-- <view class="badge" v-if="pendingReceiptCount > 0">{{ pendingReceiptCount }}</view> -->
									</uni-badge>
								</view>
								<view class="order-icon" @click="goToAfterSale">
									<uni-badge class="uni-badge-left-margin" :offset="offsetArr" :text="value"
										absolute="rightTop" size="small">
										<!-- <image class="icon" src="/static/carousel04.png" mode="aspectFit"></image> -->
										<view class="iconfont icon-shouhoudingdan all-order"></view>
										<text class="icon-text">售后订单</text>
										<!-- <view class="badge" v-if="afterSaleCount > 0">{{ afterSaleCount }}</view> -->
									</uni-badge>
								</view>
							</view>
						</view>
					</view>
					<view v-if="activeMenu == 1">
						<view class="account-section">
							<view class="right-title">
								消息中心
							</view>
							<uni-list class="messageList">
								<uni-list-item v-for="(exit,index) in messageList" :key="index" :title="exit.content"
									:rightText="exit.time" />
							</uni-list>
							<view class="no-data" v-if="!messageList || !messageList[0] || !messageList[0]._id">
								<image src="https://mp-5a81006c-e233-49f0-8a6a-cb59bcd34a71.cdn.bspapp.com/avatar/wujilu.png" mode="aspectFill" style="width:70px;height:70px;"></image>
								<view class="no-data-text">
									暂无任何消息
								</view>
							</view>
						</view>
					</view>
					<view v-if="activeMenu == 2">
						<view class="account-section">
							<view class="right-title">
								资料设置
							</view>

							<view class="modal-content">
								<uni-forms label-width="80px" ref="customForm" :rules="customRules"
									:modelValue="customFormData">
									<uni-forms-item class="uniForms-item" label="">
										<view class="uni-header">
											<image :src="userinfo.avatar" mode="aspectFill" style="width:100px;height:100px;"></image>
										</view>
										<view class="modify" @click="editAvatar()">头像修改</view>
									</uni-forms-item>

									<uni-forms-item label="真实姓名">
										
										<uni-easyinput v-model="customFormData.name" placeholder="请输入真实姓名" />
									</uni-forms-item>
									<uni-forms-item label="电子邮箱">
										<uni-easyinput v-model="customFormData.email" placeholder="请输入邮箱" />
									</uni-forms-item> 
									<uni-forms-item label="单位名称">
										<uni-easyinput v-model="customFormData.company" placeholder="请输入单位名称" />
									</uni-forms-item>
									
								</uni-forms>
								

								<view class="button-group">
									
									<button class="btn confirm" @click="submit('userinfoEdit')">确定</button>
								</view>
							</view>

						</view>
					</view>
					<view v-if="activeMenu == 3">
						<view class="account-section">
							<view class="right-title">
								收货地址
							</view>
							<view class="newlyAdded">
								<button class="add-btn" @click="ressModal">新增地址</button>
							</view>
							<view class="invoice-content">
								<radio-group @change="setAddressDefault">
									<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in address"
										:key="item.value">
										<view class="invoice-item">
											<uni-card style="min-height:125px;" :title="'收货人：'+item.name" :sub-title="item.area[0].text+item.area[1].text+item.area[2].text+item.address"
												:extra="item.mobile">
												<view class="card-actions">
													<view class="card-actions-item cardActions">
														<label class="radio">
															<radio color="#E91F1B" :value="index"
																:checked="item.is_default?true:false"/>
															<text v-if="item.is_default == true" style="color:red;">
																默认
															</text>
															<text v-else style="color:#666;">
																默认
															</text>
														</label>
													</view>
													<view class="card-actions-item cardActionsItem" @click="ressModal(item,index)">
														<uni-icons type="compose" size="18" color="#999"></uni-icons>
														<text class="card-actions-item-text">编辑</text>
													</view>
													<view class="card-actions-item" @click="openPopup(item,index)">
														<uni-icons type="trash" size="18" color="#999"></uni-icons>
														<text class="card-actions-item-text">删除</text>
													</view>
												</view>
											</uni-card>
										</view>
									</label>
								</radio-group>
								
								<view class="no-data" v-if="!address || !address[0] || !address[0]._id">
									<image src="https://mp-5a81006c-e233-49f0-8a6a-cb59bcd34a71.cdn.bspapp.com/avatar/wujilu.png" mode="aspectFill" style="width:70px;height:70px;"></image>
									<view class="no-data-text">
										暂无任何地址信息
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-if="activeMenu == 4">
						<view class="account-section">
							<view class="right-title">
								发票信息
							</view>
							<view class="newlyAdded">
								<button class="add-btn" @click="addInvoice">新增发票</button>
							</view>
							<view class="invoice-content">
								<radio-group @change="radioInvoiceChange">
									<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in invoice"
										:key="item.value">
										<view class="invoice-item">
											<uni-card :title="item.title" :sub-title="item.number">
												<view class="card-actions">
													<view class="card-actions-item cardActions">
														<label class="radio">
															<radio color="#E91F1B" :value="index"
																:checked="item.is_default?true:false"/>
															<text v-if="item.is_default == true" style="color:red;">
																默认
															</text>
															<text v-else style="color:#666;">
																默认
															</text>
														</label>
													</view>
													<view class="card-actions-item cardActionsItem" @click="addInvoice(item,index)">
														<uni-icons type="compose" size="18" color="#999"></uni-icons>
														<text class="card-actions-item-text">编辑</text>
													</view>
													<view class="card-actions-item" @click="openinvoice(item,index)">
														<uni-icons type="trash" size="18" color="#999"></uni-icons>
														<text class="card-actions-item-text">删除</text>
													</view>
												</view>
											</uni-card>
										</view>
									</label>
								</radio-group>
								<view class="no-data" v-if="!invoice || !invoice[0] || !invoice[0]._id">
									<image src="https://mp-5a81006c-e233-49f0-8a6a-cb59bcd34a71.cdn.bspapp.com/avatar/wujilu.png" mode="aspectFill" style="width:70px;height:70px;"></image>
									<view class="no-data-text">
										暂无任何发票信息
									</view>
								</view>
							</view>
						</view>
					</view>
					<view v-if="activeMenu == 5">
						<view class="account-section">
							<view class="right-title">
								修改手机号
							</view>
							<view class="modal-content">
								<uni-forms label-width="80px" ref="customForm" :rules="customRules"
									:modelValue="phoneData">

									<uni-forms-item label="新手机号">
										<view class="formsItem formsPadd">
											<uni-easyinput v-model="phoneData.mobile" placeholder="请输入手机号" />
											<view class="formsView" @click="getSmsCode" v-if="countDown <= 0">发送验证码</view>
											<view class="formsView" style="color:#999;" v-else>{{countDown}}S</view>
										</view>
									</uni-forms-item>
									<uni-forms-item label="验证码">
										<uni-easyinput type="number" maxlength="6" v-model="phoneData.code"
											placeholder="请输入验证码" />
									</uni-forms-item>

								</uni-forms>
								<view class="button-group">
									<button class="btn confirm" @click="submit('phone')">确定</button>
								</view>
							</view>
						</view>
					</view>
					<view v-if="activeMenu == 6">
						<view class="account-section">
							<view class="right-title">
								修改密码
							</view>
							<view class="modal-content">
								<uni-forms label-width="80px" ref="customForm" :rules="customRules"
									:modelValue="passwordData">
									<uni-forms-item label="登录密码">
										<uni-easyinput v-model="passwordData.password" placeholder="必须包含字母+数字，8位以上长度"
											type="password" />
									</uni-forms-item>
									<uni-forms-item label="确认密码">
										<uni-easyinput v-model="passwordData.confirmPassword" placeholder="请输入确认密码"
											type="password" />
									</uni-forms-item>
								</uni-forms>
								<view class="button-group">
									
									<button class="btn confirm" @click="submit('password')">确定</button>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 修改地址弹窗 -->
		<editAddress ref="addressModal" :defaultAddress="defaultAddress" @addressUpdated="addAddress" :userinfo="userinfo"></editAddress>
		<!-- 修改和新增发票弹窗 -->
		<editInvoice :defaultInvoice="defaultInvoice" @invoiceUpdated="setAddInvoice" :userinfo="userinfo" ref="addInvoice"></editInvoice>
		<!--删除地址弹窗-->
		<uni-popup ref="popup" type="center">
			<view class="popup-box">
				<text class="title">提示</text>
				<text class="desc">是否删除地址？</text>
				<view class="button-group">
					<button class="btn cancel" @click="closePopup">取消</button>
					<button class="btn confirm" @click="confirmDelete">确定</button>
				</view>
			</view>
		</uni-popup>
		<!--删除发票弹窗-->
		<uni-popup ref="invoice" type="center">
			<view class="popup-box">
				<text class="title">提示</text>
				<text class="desc">是否删除发票信息？</text>
				<view class="button-group">
					<button class="btn cancel" @click="closeinvoice">取消</button>
					<button class="btn confirm" @click="invoiceDelete">确定</button>
				</view>
			</view>
		</uni-popup>
		<menu-tool></menu-tool>
		<zmt-avatar-cropper @close="updateShowCrop" @confirm="uploadSuccess" :isShow="showCrop" :imageSrc="imageSrc"></zmt-avatar-cropper>
	</view>
</template>

<script>
	var db = uniCloud.database();
	export default {
		name: "dersonalCenter",
		data() {
			return {
				removeInvoice:{},
				defaultInvoice:{},//默认修改发票信息
				invoice:[],//发票数据
				defaultAddress:{},
				mobile:null,
				countDown:0,
				address:[],//地址列表
				userinfoId:null,
				showCrop:false,
				imageSrc: "",
				userinfo:{},
				menuItems: ['我的账户', '消息中心', '资料设置', '收货地址', '发票信息', '修改手机号', '修改密码'],
				activeMenu: 1, // 初始选中第一个菜单

				allOrdersCount: 52,
				pendingPaymentCount: 44,
				pendingDeliveryCount: 3,
				pendingReceiptCount: 0,
				afterSaleCount: 2,

				value: 56, // 数字角标内容
				offsetArr: [7, 3],
				// 列表数据

				loading: false, // 加载状态
				noMoreData: false, // 是否没有更多数据
				page: 1, // 当前页码
				messageList: [],
				

				// 个人资料
				// 自定义表单数据
				customFormData: {
					name: '', // 真实姓名
					email: '', // 邮箱
					company: '', // 公司信息
				},
				phoneData: {
					phone: '', // 手机号
					verification: '', // 验证码
				},
				passwordData: {
					password: '', // 新密码
					confirmPassword: '', // 确认新密码
				},
				imageStyles: {
					width: 100,
					height: 100,
					border: {
						radius: '50%'
					}
				},
				showPassword: true,

				// 收货地址
				current: -1,
				code:null

			}
		},
		
		created() {
			//获取登录用户信息
			this.getStorageUserinfo();
			//获取系统消息
			this.getMessage();
			//查询出收货地址
			this.getAddress();
			//查询发票信息
			this.getInvoice();
		},
		onReachBottom(val) {
			if (this.activeMenu === 1) {
				this.loadMoreData(); // 加载更多数据
			}
		},
		onLoad(e) {
			var that = this;
			if(e && e.type){
				that.activeMenu = e.type;
			}
		},
		methods: {
			//设置默认值
			radioInvoiceChange(item){
				var index = item.detail.value;
				var id = this.invoice[index]._id;
				var isDefault = !this.invoice[index].is_default;
				for(var i = 0;i<this.invoice.length;i++){
					this.invoice[i].is_default = false;
				}
				this.invoice[index].is_default = true;
				if(id && this.userinfoId){
					
					//先将其他默认选项设为非默认
					db.collection('invoice').where({uid:this.userinfoId}).update({is_default:false}).then(res=>{
						this.setIsDefault('invoice',isDefault,id);
					});
				}
				
			},
			//设置默认值
			setAddressDefault(item){
				var index = item.detail.value;
				var id = this.address[index]._id;
				var isDefault = !this.address[index].is_default;
				for(var i = 0;i<this.address.length;i++){
					this.address[i].is_default = false;
				}
				this.address[index].is_default = true;
				if(id && this.userinfoId){
					
					//先将其他默认选项设为非默认
					db.collection('address').where({uid:this.userinfoId}).update({is_default:false}).then(res=>{
						this.setIsDefault('address',isDefault,id);
					});
				}
				
			},
			setIsDefault(table,value,id){
				if(id){
					
					db.collection(table).where({_id:id}).update({
						is_default:value
					}).then(res=>{
						uni.hideLoading();
					})
				}
			},
			// 获取短信验证码
			getSmsCode() {
				if(this.countDown > 0){
					return false;
				}
				if (!/^1[3-9]\d{9}$/.test(this.phoneData.mobile)) {
					uni.showToast({
						title: '请输入正确手机号码',
						icon: 'none'
					})
					
					return;
				}
					uni.showLoading({
						title:'发送中'
					})
					var code = this.getSixDigitRandom();
					var login = uniCloud.importObject('login',{customUI:true});
					login.sendSms(this.phoneData.mobile,code).then(res=>{
							if(res.code == 0 && res.success == true){
								uni.hideLoading()
								//把手机号码和验证码都存到本地,防止发送完后被篡改
								this.mobile = this.phoneData.mobile;
								this.code = code;
								this.startCountDown();
								uni.showToast({
									title: '发送成功',
									icon: 'none'
								})
							}else{
								uni.hideLoading()
								uni.showToast({
									title: '发送频繁，请稍后再试',
									icon: 'none'
								})
							}
						
					}).catch(err=>{
						uni.hideLoading()
						uni.showToast({
							title: '发送频繁，请稍后再试',
							icon: 'none'
						})
					});
			
				
			},
			//生成六位随机数
			getSixDigitRandom() {
			    // 生成100000到999999之间的随机整数
			    // Math.random()生成0-1之间的随机数
			    // 乘以900000得到0-900000之间的数
			    // 加上100000确保最小值为100000
			    // Math.floor()取整数部分
			    return Math.floor(Math.random() * 900000) + 100000;
			},
			// 启动倒计时
			startCountDown() {
				this.countDown = 60
				this.timer = setInterval(() => {
					if (this.countDown <= 0) {
						clearInterval(this.timer)
						return
					}
					this.countDown--
				}, 1000)
			},
			//设置地址
			setAddInvoice(e){
				if(e && (e.index == 0 || e.index > 0)){
					this.invoice[e.index] = e.item;
				}else{
					this.invoice.unshift(e);
				}
			},
			//添加或修改地址成功以后
			addAddress(e){
				if(e && (e.index == 0 || e.index > 0)){
					this.address[e.index] = e.item;
				}else{
					this.address.unshift(e);
				}
				
			},
			//获取发票信息
			getInvoice(){
				if(this.userinfo && this.userinfo._id){
					db.collection('invoice').where({uid:this.userinfo._id}).orderBy('_id desc').get().then(res=>{
						this.invoice = res.result.data;
					});
				}
			},
			//查询出收货地址
			getAddress(){
				if(this.userinfo && this.userinfo._id){
					db.collection('address').where({uid:this.userinfo._id}).orderBy('_id desc').get().then(res=>{
						this.address = res.result.data;
					});
				}
				
			},
			//提交修改
			submit(item){
				var that = this;
				if(item == 'userinfoEdit'){
					delete this.customFormData._id;
					delete this.customFormData.mobile;
					var id = this.userinfoId;
					if(id){
						db.collection('userinfo').where({_id:id}).update(this.customFormData).then(res=>{
							uni.showToast({
								title:'修改成功'
							})
							this.userinfo = this.customFormData;
							this.userinfo._id = id;
							//重新存入缓存
							uni.setStorage({
								key:'login-userinfo',
								data:that.userinfo
							})
						});
					}
					
				}
				
				//修改手机号码
				if(item == 'phone'){
					var id = this.userinfoId;
					if (!/^1[3-9]\d{9}$/.test(this.phoneData.mobile)) {
						uni.showToast({
							title: '请输入正确手机号码',
							icon: 'none'
						})
						
						return;
					}
					if(!this.phoneData.code){
						uni.showToast({
							title:'请输入验证码',
							icon:'none'
						})
						return false;
					}
					
					
					
					if(this.code != this.phoneData.code){
						uni.showToast({
							title:'验证码错误',
							icon:'none'
						})
						return false;
					}
					
					if(this.phoneData.mobile != this.mobile){
						uni.showToast({
							title:'手机号被修改，请重新获取验证码',
							icon:'none'
						})
						return false;
					}
					
					//修改手机号码
					uni.showLoading({
						title:'修改中',
						icon:'none'
					})
					if(id){
						db.collection('userinfo').where({_id:id}).update({mobile:this.mobile}).then(res=>{
							uni.showToast({
								title:'修改成功，请重新登录',
								icon:'none'
							})
							//退出登录并跳转到登录页面
							setTimeout(function(){
								uni.hideLoading()
								uni.removeStorage({
									key:'login-userinfo',
									success:function(){
										//跳转到登录页面
										uni.reLaunch({
											url: '/pages/login/login'
										});
									}
								})
							},2000);
						});
					}
					
				}
				
				//修改密码
				if(item == 'password'){
					var password = this.passwordData.password;
					var confirmPassword = this.passwordData.confirmPassword;
					if(!this.validatePassword(password)){
						uni.showToast({
							title:'密码格式错误',
							icon:'none'
						})
						return false;
					}
					if(password != confirmPassword){
						uni.showToast({
							title:'两次密码输入不一致',
							icon:'none'
						})
						return false;
					}
					uni.showLoading({
						title:'修改中'
					})
					var userinfo = uniCloud.importObject('userinfo',{customUI:true});
					userinfo.md5(password).then(pass=>{
						var data = {
							password:pass
						}
						
						if(this.userinfoId){
							db.collection('userinfo').where({_id:this.userinfoId}).update(data).then(res=>{
								uni.hideLoading()
								uni.showToast({
									title:'修改成功'
								})
							});
						}
					});
					
				}
			},
			// 验证密码格式：必须包含字母和数字，且长度至少8位
			validatePassword(password) {
			    // 检查是否为字符串类型
			    if (typeof password !== 'string') {
			        return false;
			    }
			    
			    // 密码长度至少8位
			    if (password.length < 8) {
			        return false;
			    }
			    
			    // 正则表达式：包含至少一个字母（大小写均可）和至少一个数字
			    const hasLetter = /[a-zA-Z]/.test(password);
			    const hasNumber = /\d/.test(password);
			    
			    // 同时满足字母和数字的要求
			    return hasLetter && hasNumber;
			},
			//子组件传过来的更新父组件的状态
			updateShowCrop(e){
				this.showCrop = e;
			},
			//裁剪并上传图片以后
			uploadSuccess(e){
				var that = this;
				var avatar = e.fileID;
				if(this.userinfo && this.userinfo._id){
					//修改头像
					db.collection('userinfo').where({_id:this.userinfo._id}).update({avatar:avatar}).then(res=>{
						this.userinfo.avatar = avatar;
						uni.setStorage({
							key:'login-userinfo',
							data:that.userinfo
						})
					});
				}
				
			},
			//编辑头像
			editAvatar(){
				var that = this;
				uni.chooseImage({
					count: 1, 
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function (res) {
						that.showCrop=true;
						that.imageSrc = res.tempFilePaths[0];
					}
				});
			},
			//编辑头像
			editAvatar(){
				var that = this;
				uni.chooseImage({
					count: 1, 
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function (res) {
						that.showCrop=true;
						that.imageSrc = res.tempFilePaths[0];
					}
				});
			},
			formatTime(timestamp, format) {
			      // 同上的格式化方法
			      if (!timestamp) return ''
			      if (timestamp.toString().length === 10) {
			        timestamp = timestamp * 1000
			      }
			      
			      const date = new Date(timestamp)
			      const opt = {
			        "Y+": date.getFullYear().toString(),
			        "M+": (date.getMonth() + 1).toString(),
			        "D+": date.getDate().toString(),
			        "H+": date.getHours().toString(),
			        "m+": date.getMinutes().toString(),
			        "s+": date.getSeconds().toString()
			      }
			      
			      let result = format
			      for (let k in opt) {
			        const reg = new RegExp(`(${k})`)
			        if (reg.test(result)) {
			          result = result.replace(reg, reg.exec(result)[1].length === 1 ? 
			            opt[k].padStart(2, '0') : opt[k])
			        }
			      }
			      return result
			    },
			//获取系统消息
			getMessage(){
				db.collection('message').where({uid:this.userinfo._id}).orderBy('create_date desc').skip(0).limit(50).get().then(res=>{
					var data = res.result.data;
					for(var i = 0;i<data.length;i++){
						data[i].time = this.formatTime(data[i].create_date,'YYYY年MM月DD日 HH:mm');
					}
					
					this.messageList = data;
				});
			},
			//获取登录用户信息
			getStorageUserinfo(){
				var that = this;
				uni.getStorage({
					key:'login-userinfo',
					success:function(res){
						if(res && res.data && res.data._id){
							that.userinfo = res.data;
							that.customFormData = res.data;
							that.userinfoId = res.data._id;
						}else{
							//跳转到登录页面
							uni.navigateTo({
								url:'/pages/login/login'
							})
						}
						
					},
					fail:function(err) {
						//跳转到登录页面
						uni.navigateTo({
							url:'/pages/login/login'
						})
					}
				})
			},
			
			actionsClick(index) {
				uni.showToast({
					title: index,
					icon: 'none'
				})
			},
			changePassword() {
				this.showPassword = !this.showPassword;
			},
			// 省市县选项变化
			onchange(e) {
				const value = e.detail.value
			},
			// 点击省市县选项
			onnodeclick(node) {
				
			},
			// 打开删除发票弹窗
			openinvoice(item,index) {
				this.removeInvoice = {
					item:item,
					index:index
				}
				this.$refs.invoice.open();
			},
			// 关闭删除发票弹窗
			closeinvoice() {
				this.$refs.invoice.close();
			},
			// 打开删除地址弹窗
			openPopup(item,index) {
				this.removeAddress = {
					item:item,
					index:index
				}
				this.$refs.popup.open();
			},
			// 关闭删除地址弹窗
			closePopup() {
				this.$refs.popup.close();
			},
			// 确定删除地址按钮
			confirmDelete() {
				var that = this;
				var removeAddress = this.removeAddress;
				
				db.collection('address').where({_id:removeAddress.item._id}).remove().then(res=>{
					uni.showToast({
						title:'删除成功'
					})
					setTimeout(function(){
						that.address.splice(removeAddress.index,1);
					},2000);
					
				});
				this.closePopup();
			},
			// 确定删除发票按钮
			invoiceDelete() {
				var that = this;
				var removeInvoice = this.removeInvoice;
				if(removeInvoice && removeInvoice.item && removeInvoice.item._id){
					db.collection('invoice').where({_id:removeInvoice.item._id}).remove().then(res=>{
						uni.showToast({
							title:'删除成功'
						})
						setTimeout(function(){
							that.invoice.splice(removeInvoice.index,1);
						},2000);
						
					});
				}
				this.closeinvoice();
			},
			// 打开新增修改发票弹窗
			addInvoice(item = null,index = null) {
				console.log('item',item);
				this.defaultInvoice = {
					item:item,
					index:index
				}
				this.$refs.addInvoice.openModal()
			},
			// 打开修改地址弹窗
			ressModal(item,index) {
				this.defaultAddress = {
					item:item,
					index:index
				}
				this.$refs.addressModal.openModal()
			},
			goToAllOrders() {
				// 这里添加跳转到全部订单页面的逻辑，例如uni.navigateTo({ url: '/pages/allOrders/allOrders' })
				console.log('跳转到全部订单页面');
			},
			goToPendingPayment() {
				// 跳转到待付款页面逻辑
				console.log('跳转到待付款页面');
			},
			goToPendingDelivery() {
				// 跳转到待发货页面逻辑
				console.log('跳转到待发货页面');
			},
			goToPendingReceipt() {
				// 跳转到待收货页面逻辑
				console.log('跳转到待收货页面');
			},
			goToAfterSale() {
				// 跳转到售后订单页面逻辑
				console.log('跳转到售后订单页面');
			},
			loadMoreData() {
				if (this.loading || this.noMoreData) return;
				this.loading = true;
				// 这里可以替换成实际的API请求
				setTimeout(() => {
					let a = 0;
					if (this.messageList.length <= 100) {
						a = 1;
					} else {
						this.noMoreData = true;
					}

					if (a > 0) {
						this.messageList = this.messageList.concat(this.messageList1);
						this.page += 1;
					} else {
						this.noMoreData = true;
					}
					this.loading = false;
				}, 1000); // 模拟网络延迟
			},
		}
	}
</script>

<style scoped>
	.no-data-text{width:100%;color:#999;font-size:12px;line-height:30px;}
	.no-data{width:100%;float:left;margin-top:200px;text-align: center;}
	.uni-header{width:100px;height:100px;border:1px solid #eee;border-radius: 50%;margin-left:calc((100% - 100px)/2);overflow: hidden;}
	.user-text text:last-child{font-size:14px;color:#999;}
	.user-text text:first-child{margin-top:5px;}
	.user-text text{float:left;width:100%;line-height:20px;}
	.dersonalCenter {
		width: 100%;
		min-height: 100%;
		background-color: #F5F5F5;
	}

	/* 左侧菜单 */
	.menu-container {
		display: flex;
		margin-left: 40px;
		width: 1400px;
		/* height: 100vh; */
		margin: 30px auto 0;
	}

	.menu-sidebar {
		width: 220px;
		height: 800px;
	}

	.menu-left {
		width: 220px;
		height: 800px;
		background: #FFF;
		border-radius: 10px;
		position: sticky;
		top: 90px;
	}

	.left-menu {
		width: 100%;
		padding: 20px 0;
		box-sizing: border-box;
		font-size: 14px;
		text-align: center;
	}

	.left-menu view {
		/* padding: 15px; */
		/* width: 100px; */
		width: 100%;
		/* height: 30px; */
		height: 60px;
		line-height: 60px;
		margin: 0 auto;
		/* margin-bottom: 10px; */
		/* border-radius: 15px; */
		cursor: pointer;
		transition: background 0.3s;
	}

	.left-menu .active {
		/* background: #f3a73f; */
		background: rgb(250, 240, 226);
		border-right: 3px solid rgb(240, 209, 168);
		/* color: #FFF; */
		text-align: center;
		/* font-weight: bold; */
	}

	.menu-title {
		width: 100%;
		height: 50px;
		color: #fff;
		text-align: center;
		line-height: 50px;
		font-size: 18px;
		font-weight: bold;
		border-radius: 10px 10px 0 0;
		background-image: linear-gradient(to right, #e60012, #f3a73f);
	}

	/* 右侧内容 */
	.right-content {
		width: 1200px;
		min-height: 800px;
		padding: 14px 30px;
		box-sizing: border-box;
		background: #fff;
		margin-left: 20px;
		border-radius: 10px;
	}

	/* 收货地址 */

	.right-title {
		width: 100%;
		height: 40px;
		line-height: 40px;
		border-radius: 6px;
		/* border-radius: 25px; */
		background: #EBEBEB;
		color: #050505;
		padding: 0 5px 0 15px;
		box-sizing: border-box;
		position: sticky;
		top: 90px;
		z-index: 99;
	}

	.newlyAdded {
		width: 100%;
		height: 50px;
		/* background-color: #333; */
		display: flex;
		align-items: end;
		justify-content: end;
	}

	.add-btn {
		width: 100px;
		height: 30px;
		line-height: 30px;
		background-color: #E91F1B;
		color: #FFF;
		margin-right: 20px;
		font-size: 14px;
	}

	.invoice-content {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		/* width: 100%; */
		margin-bottom: 20px;
		/* margin-top: 20px; */
	}

	uni-radio-group {
		display: flex;
		flex-wrap: wrap;
	}

	/* .invoice-item {
		width: 480px;
		margin-right: 40px;
		height: 86px;
		border: 1px solid #ddd;
		padding: 10px;
		box-sizing: border-box;
		margin-bottom: 20px;
		cursor: pointer;
	}

	.invoice-itemSelected {
		border: 1px dashed #dc3545;
	}

	.invoice-item-content {
		display: flex;
		flex-direction: column;
	}

	.invoice-receipt {
		display: flex;
		justify-content: space-between;
		margin-top: 20px;
	}

	.default-tag {
		background-color: #ffc107;
		color: #fff;
		font-size: 12px;
		padding: 2px 6px;
		border-radius: 2px;
	}

	.sub-text {
		color: #3B4144;
		font-size: 14px;
	} */

	.invoice-item {
		width: 380px;
	}

	.invoice-item /deep/ .uni-ellipsis {
		overflow: visible;
		white-space: normal;
		text-overflow: clip;
	}

	.card-actions {
		display: flex;
		flex-direction: row;
	}

	.card-actions-item {
		display: flex;
	}

	.uni-label-pointer {
		/* margin-right: 50px; */
	}

	.cardActions {
		/* margin-left: 264px; */
		width: 190px;
	}

	.cardActionsItem {
		width: 60px;
	}

	.card-actions-item-text {
		font-size: 12px;
		color: #666;
		margin-left: 5px;
	}

	.selectCor {
		color: #E91F1B;
	}

	.invoice-item /deep/ uni-radio:not([disabled]) .uni-radio-input {
		width: 16px;
		height: 16px;
	}

	.invoice-item /deep/ uni-radio:not([disabled]) .uni-radio-input:hover {
		border-color: #E91F1B !important;
	}


	/* 我的订单部分 */
	.section-title {
		background-color: #eee;
		padding: 10px;
		border-radius: 5px;
		margin-bottom: 10px;
		font-size: 16px;
		color: #333;
	}

	.user-info {
		display: flex;
		align-items: center;
		margin-bottom: 20px;
	}

	.user-avatar {
		width: 80px;
		height: 80px;
		border-radius: 50%;
		margin: 20px;
	}

	.user-text {
		font-size: 16px;
	}

	.order-icons {
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap;
		margin-top: 40px;
	}

	.order-icon {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 20px;
		width: 150px;
	}

	.order-icon /deep/ .uni-badge--error {
		background-color: #e60012;
		min-width: 26px;
		height: 26px;
		line-height: 24px;
		font-size: 14px;
	}

	.all-order {
		font-size: 70px;
		color: #666666;
	}

	.icon {
		width: 50px;
		height: 50px;
		margin-bottom: 5px;
	}

	.icon-text {
		font-size: 14px;
		color: #666;
	}

	.badge {
		position: absolute;
		top: -5px;
		right: -5px;
		background-color: red;
		color: white;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 50%;
		font-size: 12px;
	}

	/* 消息列表 */
	.messageList {
		width: 100%;
		min-height: 660px;
		overflow-y: auto;
		margin-top: 20px;
	}

	.loading {
		text-align: center;
		padding: 0 0 70px 0;
		color: #999;
	}

	.no-more-data {
		text-align: center;
		padding: 0 0 70px 0;
		color: #999;
	}

	/* 个人资料 */
	.modal-content {
		background-color: white;
		width: 520px;
		height: 390px;
		padding: 20px;
		box-sizing: border-box;
		border-radius: 8px;
		margin:30px;
	}

	.uniForms-item /deep/ .uni-forms-item__label {
		margin-top: 10px;
	}

	.uniForms-item /deep/ .file-picker__box {
		margin: auto;
	}

	.modify {
		text-align: center;
		font-size:13px;
		color:#999;
		cursor:pointer;
	}

	.search-icon {
		font-size: 70px;
	}


	/* 按钮 */

	/* uni-button[type='primary'] {
		background-color: #D92400;
	} */

	.button-group {
		display: flex;
		justify-content: space-between;
		margin: 30px auto;
		margin-bottom: 0;
		width: 290px;
	}

	.btn {
		width: 25%;
		height: 30px;
		border-radius: 8px;
		font-size: 16px;
		line-height: 30px;
		font-size: 14px;
		/* margin-left: 60px; */
	}

	.btn.cancel {
		background: #f5f5f5;
		color: #333;
		/* margin-left: 120px; */
		/* margin-right: 20px; */
	}

	.btn.confirm {
		background: #E91F1B;
		color: #fff;
		margin-left: 0;
		margin-right:30px;
	}

	/* 修改手机号 */
	.formsItem {
		display: flex;
		position: relative;
	}

	.formsPadd /deep/ .uni-input-input {
		padding-right: 100px;
		box-sizing: border-box;
	}

	.formsPadd /deep/ .content-clear-icon {
		display: none;
	}

	.formsView {
		margin-left: 10px;
		line-height: 36px;
		cursor: pointer;
		position: absolute;
		top: 0;
		right: 16px;
	}

	.formsView:hover {
		color: #E91F1B;
	}

	/* 删除地址弹窗 */
	.popup-box {
		width: 300px;
		/* 弹窗宽度 */
		padding: 20px;
		background-color: #fff;
		border-radius: 8px;
		box-sizing: border-box;
		overflow: hidden;
	}

	.title {
		font-size: 15px;
		font-weight: bold;
		margin-bottom: 15px;
		display: block;
	}

	.desc {
		font-size: 14px;
		color: #666;
		line-height: 1.5;
		margin-bottom: 25px;
		display: block;
	}

	.desc1 {
		font-size: 14px;
		color: #333;
		line-height: 1.5;
		display: block;
	}

	.descSize {
		font-size: 12px;
		color: #999;
	}

	.confirm-btn {
		float: right;
		padding: 0 15px;
		background-color: #f5f5f5;
		color: #333;
		border-radius: 4px;
		font-size: 14px;
	}

	.button-group {
		display: flex;
		/* justify-content: space-between; */
		margin: 30px auto;
		margin-bottom: 0;
		width: 290px;
	}

	.btn {
		width: 30%;
		height: 30px;
		border-radius: 8px;
		font-size: 16px;
		line-height: 30px;
		font-size: 14px;
		/* margin-left: 60px; */
	}

	.btn.cancel {
		background: #f5f5f5;
		color: #333;
		margin-left: 120px;
		margin-right: 20px;
	}

	.btn.confirm {
		background: #E91F1B;
		color: #fff;
		margin-left: 0;
	}
</style>